import http from "@/utils/httpRequest";
import { Message } from "element-ui";

/**
 * 排序
 */
export function compare(property, type) {
  return function(a, b) {
    // 1升序,0降序
    var value1 = a[property];
    var value2 = b[property];
    return type === 0 ? value2 - value1 : value1 - value2;
  };
}

// 根据字典名称数据列表
export function getParamsType(name, type) {
  return new Promise((resolve, reject) => {
    http({
      url: http.adornUrl("/sys/dict/select"),
      method: "post",
      params: {
        name: name
      }
    }).then(({ data }) => {
      if (data && data.code === 0) {
        var dict = data.list ? data.list[0].dictDetails : [];
        if (type === 1) {
          var detailMap = {};
          dict.forEach(element => {
            detailMap[element.value] = element.label;
          });
          resolve(detailMap);
        } else {
          resolve(dict); // Array
        }
      } else {
        Message({ type: "warning", message: data.msg });
        if (type === 1) {
          resolve({});
        } else {
          resolve([]); // Array
        }
      }
    });
  });
}

/**
 * 某元素全屏放大
 * @param {*} id 元素id
 * @param {*} fullscreen 是否全屏
 * @returns 是否全屏
 */
 export function fullscreenById (id, fullscreen) {
  let element = document.getElementById(id)
  if (fullscreen) {
    // 如果已经全屏了就退出全屏
    if (document.exitFullscreen) {
      document.exitFullscreen()
    } else if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen()
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen()
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen()
    }
    return !fullscreen
  } else {
    // 如果不是全屏就变成全屏
    if (element.requestFullscreen) {
      element.requestFullscreen()
    } else if (element.webkitRequestFullScreen) {
      element.webkitRequestFullScreen()
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen()
    } else if (element.msRequestFullscreen) {
      // IE11
      element.msRequestFullscreen()
    }
    return !fullscreen
  }
}

export function getUrl() {
  if (window.location.href.indexOf("dist") !== -1) {
    return window.location.href.split("dist")[0];
  } else if (window.location.href.indexOf("web") !== -1) {
    return window.location.href.split("web")[0];
  } else {
    return window.location.href.split("#/")[0];
  }
}
/* 获取项目路径 */
export function getFileUrl(value, type = null) {
  if (value) {
    var index = value.lastIndexOf("/resource");
    var paramValue = value.substring(index + 1, value.length);
    if (type) {
      // 测试
      return "http://192.168.97.6:8089/" + paramValue;
    } else {
      if (window.location.href.indexOf("dist") !== -1) {
        return window.location.href.split("dist")[0] + paramValue;
      } else if (window.location.href.indexOf("web") !== -1) {
        return window.location.href.split("web")[0] + paramValue;
      } else {
        return "http://192.168.97.6:8089/" + paramValue;
        // return window.location.href.split('#/')[0] + paramValue
      }
    }
  } else {
    return null;
  }
}
